home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 008 / bonus.arc / SQR.LSP < prev    next >
Encoding:
Lisp/Scheme  |  1985-01-28  |  941 b   |  29 lines

  1. ;
  2. ;  This is an implementation of a square root function in
  3. ;  LISP using the Newton-Raphson method as used in AutoCAD.
  4. ;  It is intended as a test of floating point arithmetic in
  5. ;  our LISP, as you can check accuracy with the statement:
  6. ;     (- (sqr 2) (sqrt 2))
  7. ;  which will compare the built-in function with this one.
  8. ;
  9. ;  John Walker  12/17/84
  10. ;
  11. (defun sqr (x)
  12.         (cond ((minusp x) 'Negative-argument)
  13.               ((zerop x) 0.0)
  14.               (t (setq y (/ (+ 0.154116 (* x 1.893872))
  15.                                    (+ 1.0 (* x 1.047988))
  16.                          )
  17.                  )
  18.                  (setq c (/ (- y (/ x y)) 2.0))
  19.                  (setq cl 0.0)
  20.                  (while (not (equal c cl))
  21.                     (setq y (- y c))
  22.                     (setq cl c)
  23.                     (setq c (/ (- y (/ x y)) 2.0))
  24.                  )
  25.                  y
  26.               )
  27.         )
  28. )
  29.